package com.anbinh.doctor.fragment.account;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.anbinh.doctor.DetailtActivity;
import com.anbinh.doctor.R;
import com.anbinh.doctor.model.DataResponse;
import com.anbinh.doctor.model.UserLogin;
import com.anbinh.doctor.request.controller.ServerController;
import com.anbinh.doctor.service.request.OnRequest;
import com.anbinh.doctor.utils.Utils;
import com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar;

public class FragmentAddBaiviet extends SherlockFragment {
	private static FragmentAddBaiviet fragment = null;
	private View mView;
	private CircleProgressBar loading;
	private EditText edTitle;
	private EditText edLink;

	public static FragmentAddBaiviet newInstance() {
		if (fragment == null) {
			fragment = new FragmentAddBaiviet();
		}
		return fragment;
	}

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setHasOptionsMenu(true);
	}

	@Override
	public void onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu,
			com.actionbarsherlock.view.MenuInflater inflater) {
		menu.add(0, R.id.dia_diem_kham, Menu.NONE, "Hoàn thành")
				.setIcon(R.drawable.ic_done)
				.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
		super.onCreateOptionsMenu(menu, inflater);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		addBaiviet();
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onResume() {
		super.onResume();
		((DetailtActivity) getActivity()).setActionBarTitle(getResources()
				.getString(R.string.them_bai_viet));

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.fragment_them_bai_viet, null);

		return mView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		loading = (CircleProgressBar) mView.findViewById(R.id.loadding);
		edTitle = (EditText) mView.findViewById(R.id.edTitle);
		edLink = (EditText) mView.findViewById(R.id.link);

	}

	public void addBaiviet() {
		UserLogin userLogin = Utils.getAccountModel(getActivity(),
				Utils.KEY_USER);
		String token = userLogin.getData().getToken();
		String mTitle = edTitle.getText().toString().trim();
		if (mTitle.isEmpty()) {
			Utils.showMessage(
					getActivity(),
					getResources().getString(
							R.string.vui_long_nhap_ten_bai_viet));
			return;
		}
		String mLinks = edLink.getText().toString().trim();
		if (!mLinks.isEmpty() && !verifyUrl(mLinks)) {
			Utils.showMessage(getActivity(),
					getResources().getString(R.string.vui_long_nhap_link));
			return;
		}
		loading.setVisibility(View.VISIBLE);

		ServerController.getInstance(getActivity()).requestAddBaiViet(token,
				mTitle, mLinks, -1, new OnRequest<DataResponse>() {

					@Override
					public void onSuccess(DataResponse data) {
						loading.setVisibility(View.INVISIBLE);

						if (data.getResponsecode() >= 0) {
							Intent intent = new Intent();
							intent.putExtra(Utils.KEY, Utils.RESUME_EXTRA_INFO);
							getActivity().setResult(Integer.MAX_VALUE, intent);
							getActivity().finish();
						}

						Utils.showMessage(getActivity(), data.getDescription());
					}

					@Override
					public void onError(int errorCode, String message) {
						loading.setVisibility(View.INVISIBLE);

					}

				});

	}

	private boolean verifyUrl(String url) {
		String urlRegex = "^(http|https)://[-a-zA-Z0-9+&@#/%?=~_|,!:.;]*[-a-zA-Z0-9+@#/%=&_|]";
		Pattern pattern = Pattern.compile(urlRegex);
		Matcher m = pattern.matcher(url);
		if (m.matches()) {
			return true;
		} else {
			return false;
		}
	}
}
